//
//  EformViewController.m
//  FMS
//
//  Created by Steven on 11/7/13.
//  Copyright (c) 2013 QGSVN. All rights reserved.
//

#import "EformViewController.h"

@interface EformViewController ()

@end

@implementation EformViewController
@synthesize mainWebView;
@synthesize eFormDict;
@synthesize myActivityView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        dictLanguage = [ApplicationDelegate.arrayLanguage objectAtIndex:0];
    }
    return self;
}

- (IBAction)backPreviousView:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)saveEform:(id)sender {
    NSMutableDictionary *arguments = [NSMutableDictionary dictionaryWithObjectsAndKeys:[eFormDict objectForKey:@"maintenanceId"],@"idMaintenance",[eFormDict objectForKey:@"key"],@"type", nil];
    
    [[FMSServerAPI shareClient] saveEform:arguments successs:^(id success) {
        NSLog(@"response = %@",(NSMutableDictionary*)success);
    } failure:^(NSError *error) {
        
    }];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    myActivityView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(512, 368, 30, 30)];
    myActivityView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    myActivityView.hidesWhenStopped = YES;
    [myActivityView startAnimating];
    [self.view addSubview:myActivityView];
}

- (void) reloadEform {
    _lbTitle.text = [[eFormDict objectForKey:@"value"] objectForKey:@"name"];
    NSString *htmlContent = [[eFormDict objectForKey:@"value"] objectForKey:@"content"];
    [mainWebView loadHTMLString:htmlContent baseURL:nil];
    mainWebView.scalesPageToFit = YES;
}

- (void) getDetailEform:(NSString*)content {
    [mainWebView loadHTMLString:content baseURL:nil];
    mainWebView.scalesPageToFit = YES;
}

- (void)webViewDidStartLoad:(UIWebView *)webView {
    NSLog(@"start = %@",webView.request.URL.absoluteString);
    [myActivityView startAnimating];
//    mainWebView.hidden = YES;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [myActivityView stopAnimating];
    mainWebView.hidden = NO;
    if ([webView.request.URL.absoluteString rangeOfString:@"api/maintenance/saveEformMaintenance"].location != NSNotFound) {
        NSString *html = [webView stringByEvaluatingJavaScriptFromString:
                          @"document.body.innerHTML"];
        if ([html rangeOfString:@"success"].location != NSNotFound || [html rangeOfString:@"proxy"].location != NSNotFound) {
            UIAlertView *dialog = [[UIAlertView alloc] initWithTitle:nil message:[dictLanguage objectForKey:@"maintenance-mgmt.save-eform.success"] delegate:nil cancelButtonTitle:[dictLanguage objectForKey:@"general.ok"] otherButtonTitles:nil];
            [dialog show];
            [self.navigationController popViewControllerAnimated:YES];
        }
        else {
            UIAlertView *dialog = [[UIAlertView alloc] initWithTitle:nil message:[dictLanguage objectForKey:@"maintenance-mgmt.save-eform.fail"] delegate:nil cancelButtonTitle:[dictLanguage objectForKey:@"general.ok"] otherButtonTitles:nil];
            [dialog show];
            
            NSString *htmlContent = [[eFormDict objectForKey:@"value"] objectForKey:@"content"];
            [mainWebView loadHTMLString:htmlContent baseURL:nil];
        }
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
